<?php
class VideoController extends CpController
{
    public function init(){
        $url = new Url();
        $this->breadcrumbs[] = array('name'=>'QT Video','link'=>$url->createUrl("video/index"),'class'=>'');
    }
    public function actionIndex(){
        $row_per_page = 20; 
        $this->breadcrumbs[] = array('name'=>'DS video','link'=>'#','class'=>'active');
        $from_date = isset($_GET["from_date"]) ? trim($_GET["from_date"]):"";
        $to_date = isset($_GET["to_date"]) ? trim($_GET["to_date"]):"";
        $categoryId = isset($_GET["categoryId"]) ? intval($_GET["categoryId"]):0;
        $keyword = isset($_GET["keyword"]) ? trim($_GET["keyword"]):"";
        $type = isset($_GET["type"]) ? intval($_GET["type"]):0;
        $status = isset($_GET["status"]) ? intval($_GET["status"]):2;
        $isHot = isset($_GET["isHot"]) ? intval($_GET["isHot"]):2;
        $page = isset($_GET["page"]) ? intval($_GET["page"]):1;
        $data_cat = ACategory::getAllData(2);
        list($max_page,$count,$data) = AVideo::getDataSearch($from_date,$to_date,$categoryId,$keyword,$type,$status,$isHot,$page,$row_per_page);

        $this->render("index",array("data_cat"=>$data_cat,"max_page"=>$max_page,"count"=>$count,"data"=>$data,
        "from_date"=>$from_date,"to_date"=>$to_date,"categoryId"=>$categoryId,"keyword"=>$keyword,"type"=>$type,
        "status"=>$status,"isHot"=>$isHot,"page"=>$page));
    }
    public function actionCreate(){
        $this->breadcrumbs[] = array('name'=>'Thêm mới video','link'=>'#','class'=>'active');
        $data_cat = ACategory::getAllData(2);
        $this->render("create",array("data_cat"=>$data_cat));
    }

    public function actionAjaxSaveVideo(){
        $time_now = time();     
        $create_user = Yii::app()->user->name;       
        $categoryId = isset($_POST["categoryId"]) ? intval($_POST["categoryId"]) : 0; 
        $title_video = isset($_POST["title_video"]) ? trim(strip_tags($_POST["title_video"])) : ""; 
        $title_sms = isset($_POST["title_sms"]) ? trim(strip_tags($_POST["title_sms"])) : ""; 
        $picture = isset($_POST["picture"]) ? trim($_POST["picture"]) : ""; 
        $file_3gp = isset($_POST["file_3gp"]) ? trim($_POST["file_3gp"]) : ""; 
        $file_mp4 = isset($_POST["file_mp4"]) ? trim($_POST["file_mp4"]) : ""; 
        $introtext = isset($_POST["introtext"]) ? trim(strip_tags($_POST["introtext"])) : ""; 
        $status = isset($_POST["status"]) ? intval($_POST["status"]) : 0; 
        $position = isset($_POST["position"]) ? intval($_POST["position"]) : 0; 
        $duration = isset($_POST["duration"]) ? trim(strip_tags($_POST["duration"])) : ""; 
        $tag = isset($_POST["tag"]) ? trim(strip_tags($_POST["tag"])) : ""; 
        if($title_video == ''){ 
            echo "Tên video không được để trống";
            exit;
        }
        
        if($title_sms == ""){
            echo "Tiêu đề tin nhắn không được để trống";
            exit;
        }
        
        if(Common::generate_slug($title_video)==''){
            echo "Tên video không hợp lệ";
            exit;
        }
        
        if($categoryId == 0){ 
            echo "Danh mục không được để trống";
            exit;
        }
        
        if($picture == ''){ 
            echo "Chưa up ảnh";
            exit;
        }
        
        if($file_3gp == '' || $file_mp4 == ''){ 
            echo "Chưa upload file video";
            exit;
        }
        
        $data_cat = ACategory::getDataById($categoryId);
        $data = array(
            "categoryId"=>array(
                "value"=>$categoryId
                , "type"=>1
            ),
            "title"=>array(
                "value"=>$title_video
                , "type"=>2
            ),
            "title_sms"=>array(
                "value"=>$title_sms
                , "type"=>2
            ),
            "alias"=>array(
                "value"=>Common::generate_slug($title_video)
                , "type"=>2
            ),
            "picture"=>array(
                "value"=>$picture
                , "type"=>2
            ),
            "file"=>array(
                "value"=>$file_3gp
                , "type"=>2
            ),
            "file_mp4"=>array(
                "value"=>$file_mp4
                , "type"=>2
            ),
            "duration"=>array(
                "value"=>$duration
                , "type"=>2
            ),
            "introtext"=>array(
                "value"=>$introtext
                , "type"=>2
            ),
            "position"=>array(
                "value"=>$position
                , "type"=>1
            ),    
            "status"=>array(
                "value"=>$status
                , "type"=>1
            ),    
            "create_date"=>array(
                "value"=>$time_now
                , "type"=>1
            ),    
            "update_date"=>array(
                "value"=>$time_now
                , "type"=>1
            ), 
            "create_user"=>array(
                "value"=>$create_user
                , "type"=>2
            ),     
        );
        $table = "c_video";
        $videoId = CommonDB::insertRow($table,$data);
        /* them trong bang c_data*/        
        $data_c = array(
            "categoryId"=>array("value"=>$data_cat["id"],"type"=>1),
            "type"=>array("value"=>$data_cat["type"],"type"=>1),
            "dataId"=>array("value"=>$videoId,"type"=>1),
            "status"=>array("value"=>$status,"type"=>1),
            "create_date"=>array("value"=>$time_now,"type"=>1),
            "update_date"=>array("value"=>$time_now,"type"=>1),        
        );
        $table = "c_data";
        $data_id = CommonDB::insertRow($table,$data_c);
        
        if($videoId >0){
            /* Xử lý tag */
            if(!empty($tag)){
                $tag = explode(";", $tag);
                $result = ATag::InsertTag($tag, 2, $videoId);
            }
                
            $act = 1;
            $obj = 4;
            $content = LogConfig::$action[$act].' '.LogConfig::$object[$obj].': {id: '.$videoId.',name: '.$title_video.',categoryId: '.$categoryId.'}';
            $result = AAdminLog::insertAdminLog($act,$obj,$content);
            echo 1;
        }else{
            echo "Có lỗi trong quá trình xử lý! Hãy báo với kỹ thuật";exit;
        }
    }

    public function actionAjaxQuickUpdate(){
        $time_now = time();
        $status = isset($_POST["status"]) ? intval($_POST["status"]):0;
        $isHot = isset($_POST["isHot"]) ? intval($_POST["isHot"]):0;
        $isHome = isset($_POST["isHome"]) ? intval($_POST["isHome"]):0;
        $isHotApp = isset($_POST["isHotApp"]) ? intval($_POST["isHotApp"]):0;
        $id = isset($_POST["id"]) ? intval($_POST["id"]):0;
        if($id >0){
            $data_old = AVideo::getDataById($id);
            $result = AData::updateStatusByDataId($id,$status);
            $data = array(
            "isHot"=>array("value"=>$isHot,"type"=>1),
            "isHome"=>array("value"=>$isHome,"type"=>1),
            "isHotApp"=>array("value"=>$isHotApp,"type"=>1),
            "status"=>array("value"=>$status,"type"=>1),
            "id"=>array("value"=>$id,"type"=>1)
            );
            if($isHot ==1){
                $data["date_of_hot"] = array("value"=>$time_now,"type"=>1);
            }
            if($isHome ==1){
                $data["date_of_home"] = array("value"=>$time_now,"type"=>1);
            }
            if($isHotApp ==1){
                $data["date_of_hot_app"] = array("value"=>$time_now,"type"=>1);
            }
            $table = "c_video";
            $result = CommonDB::updateRow($table,$data,array("id"));
            if($result >0){
                $act = 2;
                $obj = 4;
                $content = LogConfig::$action[$act].' '.LogConfig::$object[$obj].': {id: '.$id.',name: '.$data_old["title"].',isHot: '.$data_old["isHot"].',isHome: '.$data_old["isHome"].',isHotApp: '.$data_old["isHotApp"].',status: '.$data_old["status"].'} thành';
                $content .= ' {id: '.$id.',name: '.$data_old["title"].',isHot: '.$isHot.',isHome: '.$isHome.',isHotApp: '.$isHotApp.',status: '.$status.'}';
                $result = AAdminLog::insertAdminLog($act,$obj,$content);
                echo 1;exit();
            }else{
                echo "Chưa thay đổi dữ liệu";
            }
        }
    }
    public function actionEdit(){
        $id = isset($_GET["id"]) ? intval($_GET["id"]):0;
        $data = AVideo::getDataById($id);
        $this->breadcrumbs[] = array('name'=>'Sửa video '.$data["title"],'link'=>'#','class'=>'active');
        if($data){
            $data_cat = ACategory::getAllData(2);
            $this->render("edit",array("data"=>$data,"data_cat"=>$data_cat));
        }
    }

    public function actionAjaxUpdateVideo(){
        $time_now = time();
        $id = isset($_POST["id"]) ? intval($_POST["id"]) : 0;
        $title_video = isset($_POST["title_video"]) ? trim(strip_tags($_POST["title_video"])) : "";
        $title_sms = isset($_POST["title_sms"]) ? trim(strip_tags($_POST["title_sms"])) : "";
        $categoryId = isset($_POST["categoryId"]) ? intval($_POST["categoryId"]) : 0;
        $position = isset($_POST["position"]) ? intval($_POST["position"]) : 0;
        $picture = isset($_POST["picture"]) ? trim($_POST["picture"]) : "";
        $file_3gp = isset($_POST["file_3gp"]) ? trim($_POST["file_3gp"]) : "";
        $file_mp4 = isset($_POST["file_mp4"]) ? trim($_POST["file_mp4"]) : "";
        $duration = isset($_POST["duration"]) ? trim(strip_tags($_POST["duration"])) : "";
        $introtext = isset($_POST["introtext"]) ? trim(strip_tags($_POST["introtext"])) : "";
        $tag = isset($_POST["tag"]) ? trim(strip_tags($_POST["tag"])) : "";
        if($title_video == '') { 
            echo "Tên video không được để trống";
            exit;
        }
        
        if($title_sms == ""){
            echo "Tiêu đề tin nhắn không được để trống";
            exit;
        }
        
        if($categoryId == 0){ 
            echo "Danh mục không được để trống";
            exit;
        }
        
        if($picture == ''){ 
            echo "Chưa up ảnh";
            exit;
        }
        
        if($file_3gp == '' || $file_mp4 == ''){ 
            echo "Chưa upload file video";
            exit;
        }
        
        $data_video = AVideo::getDataById($id);
        if($data_video){
            if($data_video["categoryId"] != $categoryId){
                $result = AData::updateCategoryByDataId($id,$categoryId);
            }
            $data = array(
                "categoryId"=>array(
                    "value"=>$categoryId
                    , "type"=>1
                ),
                "title"=>array(
                    "value"=>$title_video
                    , "type"=>2
                ),
                "title_sms"=>array(
                    "value"=>$title_sms
                    , "type"=>2
                ),
                "picture"=>array(
                    "value"=>$picture
                    , "type"=>2
                ),
                "file"=>array(
                    "value"=>$file_3gp
                    , "type"=>2
                ),
                "file_mp4"=>array(
                    "value"=>$file_mp4
                    , "type"=>2
                ),
                "duration"=>array(
                    "value"=>$duration
                    , "type"=>2
                ),
                "introtext"=>array(
                    "value"=>$introtext
                    , "type"=>2
                ),
                "position"=>array(
                    "value"=>$position
                    , "type"=>1
                ),
                "update_date"=>array(
                    "value"=>$time_now
                    , "type"=>1
                ),
                "id"=>array(
                    "value"=>$id
                    , "type"=>1
                )
            );
            $table = "c_video";
            $result = CommonDB::updateRow($table,$data,array("id"));
            if($result >0){
                /* Xử lý tag */
                $result = ATag::UpdateTag($tag, 2, $id);                
                
                $act = 2;
                $obj = 4;
                $content = LogConfig::$action[$act] . ' ' . LogConfig::$object[$obj] . ': {id: ' . $id . ',name: ' . $data_video["title"] . ',categoryId: ' . $data_video["categoryId"] . '} thành ';
                $content .= ' {id: ' . $id . ',name: ' . $title_video . ',categoryId: ' . $categoryId . '}';
                $result = AAdminLog::insertAdminLog($act,$obj,$content);
                echo 1;
                exit();
            }else{
                echo "Chưa thay đổi dữ liệu";
            }
        }
    }
    public function actionAjaxDelete(){
        $time_now = time();
        $id = isset($_POST["id"]) ? intval($_POST["id"]):0;
        if($id >0){
            $data_old = AVideo::getDataById($id);
            $table  = "c_data";
            $result = CommonDB::deleteRow($table,array("dataId"=>$id));
            $table = "c_video";
            $result = CommonDB::deleteRow($table,array("id"=>$id));
            if($result >0){
                $act = 3;
                $obj = 4;
                $content = LogConfig::$action[$act].' '.LogConfig::$object[$obj].': {id: '.$id.',name: '.$data_old["title"].',categoryId: '.$data_old["categoryId"].'}';                
                $result = AAdminLog::insertAdminLog($act,$obj,$content);
                echo 1;exit();
            }else{
                echo "Chưa thay đổi dữ liệu";
            }
        }
    }
    public function actionShow(){
        $link = isset($_GET["link"]) ? trim($_GET["link"]):"";
        $this->breadcrumbs[] = array('name'=>'Xem thử video ','link'=>'#','class'=>'active');
        if($link !=""){
            $this->render("show",array("link"=>$link));
        }
    }
}
